package tool;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import org.apache.commons.codec.binary.Base64;
import org.springframework.util.StringUtils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FsRobot {

    static String FSwebhook="";
    static String FSsecret="";

    //?????????10s
    public static final int TIME_OUT = 10000;

    /**
     * ????????????????https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
     *
     * @param webhook
     * @param secret     ??????? 3?1????????????????? ?? ???????????? ?????????????????? secret??SE??????????????IP??????????
     * @param content    ????????
     * @return
     */
    public static String sendMsg(String webhook, String secret, String content) {
        try {
            //????????????????????????webhook??
            if (!StringUtils.isEmpty(secret)) {
                webhook = new StringBuilder(webhook).toString();
            }
//            System.out.println("webhook:" + webhook);

            //???????????
            String reqStr = buildReqStr(content,secret);
            System.out.println(reqStr);
            //?????????http????
            String result = postJson(webhook, reqStr);
//            log.info("??????result == " + result);
            return result;
        } catch (Exception e) {
//            log.info("????????? ?????{}", e.getStackTrace());
            return null;
        }
    }

    /**
     * ?????????
     * ??????????? text
     *
     * @param content
     * @return
     */
    private static String buildReqStr(String content,String secret) throws Exception{

        long timestamp =System.currentTimeMillis();
        String sign = GenSign( secret,timestamp);
        System.out.println(sign);
        //???????
        Map<String, String> contentMap = new HashMap<>();
        contentMap.put("text", "<at user_id=\"all\">所有人</at> "+"<at user_id=\"7081474668712247298\">Je君恩</at> "+content);
        //????
        Map<String, Object> atMap = new HashMap<>();
        //1.???????????
        atMap.put("user_id", "all");
        atMap.put("user_name", "所有人");
        atMap.put("tag", "at");
        //2.???????????????????
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("msg_type", "text");
        reqMap.put("content", contentMap);
        reqMap.put("timestamp",timestamp/1000);
        reqMap.put("sign",sign);
        return JSON.toJSONString(reqMap);
    }


    private static String postJson(String url, String reqStr) {
        String body = null;
        try {
            body = HttpRequest.post(url).body(reqStr).timeout(TIME_OUT).execute().body();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return body;
    }

    private static String GenSign(String secret, long timestamp) throws NoSuchAlgorithmException, InvalidKeyException {
        //把timestamp+"\n"+密钥当做签名字符串
        String stringToSign = timestamp/1000 + "\n" + secret;

        //使用HmacSHA256算法计算签名
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(stringToSign.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
        byte[] signData = mac.doFinal(new byte[]{});
        return new String(Base64.encodeBase64(signData));
    }


    public static void main(String[] args) {
        //7081474668712247298
        List<String> mobileList = Lists.newArrayList();
        mobileList.add("7081474668712247298");
        String content=HttpRequest.get("http://www.weather.com.cn/data/cityinfo/101230201.html").timeout(TIME_OUT).execute().body();

        System.out.println(sendMsg(FSwebhook, FSsecret, content));
    }

}
